<template>
  <el-dialog
    top="30px"
    width="900px"
    append-to-body
    :close-on-click-modal="false"
    :before-close="beforeClose"
    :visible.sync="visible"
  >
    <el-form class="query-table-form" @submit.native.prevent :inline="true" :model="params">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="项目名称查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.corp_name"
          placeholder="负责人查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          clearable
          placeholder="地址查询"
          auto-top
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>

    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <el-tooltip effect="light" :content="scope.row.address">
          <div class="text-truncate">{{ scope.row.address }}</div>
        </el-tooltip>
      </template>
      <template v-slot:name="scope">
        <el-popover placement="top-start" title="项目经理" trigger="hover">
          <div>
            <div>{{ `A角： ${scope.row.res_a_name || '-'}；` }}</div>
            <div class="mb-2">{{ `电话： ${scope.row.res_a_phone || '-'}` }}</div>
            <div>{{ `B角： ${scope.row.res_b_name || '-'}；` }}</div>
            <div>{{ `电话： ${scope.row.res_b_phone || '-'}` }}</div>
          </div>
          <div slot="reference">
            <div class="status-label badge-info text-left w-100">
              <div>A角:{{ scope.row.res_a_name }}</div>
              <div>B角:{{ scope.row.res_b_name }}</div>
            </div>
          </div>
        </el-popover>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>
    <un-edit
      :visible.sync="editVisible"
      :table-id="tableId"
      :query-id="queryId"
      :form-data="formData"
      @success="handleSuccess"
    ></un-edit>
  </el-dialog>
</template>

<script>
import SelectAddress from '@/components/select/SelectAddress'
import { mapGetters } from 'vuex'
import UnEdit from '@/credit/views/county/loan/component/advance/UnEdit'
export default {
  components: { SelectAddress, UnEdit },
  props: {
    visible: Boolean,
  },
  data() {
    return {
      order: {
        load_end_date: 'asc',
      },
      params: {
        index: 1,
        size: 10,
      },
      loading: false,
      tableName: 'un_gua_apply_advance_view',
      tableData: [],
      tableColumns: [
        {
          attrs: { label: '客户名称', prop: 'customer_name', minWidth: '110px' },
          process: (v, row) => ({
            attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
          }),
        },
        { attrs: { label: '地址', prop: 'address' }, slot: 'address' },
        { attrs: { label: '联系电话', prop: 'mobile_no', align: 'center', width: 120 } },
        { attrs: { label: '放款金额(万元)', prop: 'reply_funds', align: 'center', width: 90 } },
        { attrs: { label: '放款时间', prop: 'startDate', align: 'center', width: 100 } },
        { attrs: { label: '到期时间', prop: 'endDate', align: 'center', width: 100 } },
        { attrs: { label: '在保余额(万元)', prop: 'left_funds', align: 'center', width: 90 } },
        // { attrs: { label: '项目经理', prop: 'resName', width: 100 }, slot: 'name' },
        {
          attrs: { label: '操作', align: 'center', width: '100px' },
          operations: [
            {
              content: '解保',
              icon: 'fa fa-edit',
              event: this.hanleEdit,
              // ifRender: row =>
              //   +row.gua_type === 2 &&
              //   ((+row.un_gua_status === 0 && this.isCemp) || row.task_person === this.userId),
            },
          ],
        },
      ],
      totalCount: 0,
      dateFmt: 'yyyy-MM-dd',
      editVisible: false,
      tableId: '',
      formData: {},
      queryId: '',
    }
  },
  computed: {
    ...mapGetters(['clientId', 'userId']),
  },
  watch: {},
  created() {
    this.params['service_id.eq'] = this.clientId
    this.params['orWhere.res_a.eq'] = this.userId
    this.params['orWhere.res_b.eq'] = this.userId
    this.getData()
  },
  mounted() {},
  methods: {
    handleSuccess() {
      this.$emit('success')
      this.getData()
    },
    hanleEdit(row) {
      this.tableId = ''
      this.editVisible = true
      this.queryId = row.customer_related_id || ''
      this.formData = {}
    },
    getData(index = 1) {
      this.params.index = index
      this.loading = true
      this.$api
        .getData(this.$serviceName, this.tableName, this.params, this.order)
        .then(res => {
          const data = res.data ?? {}
          this.tableData = (data?.data ?? []).map(this.dataAdapter)
          this.totalCount = data?.totalCount ?? 0
        })
        .finally(() => {
          this.loading = false
        })
    },
    dataAdapter(item) {
      item.endDate = this.$utils.dateFormat(item.load_end_date, this.dateFmt)
      item.startDate = this.$utils.dateFormat(item.load_date, this.dateFmt)
      // item.resName = `A角: ${item.res_a_name} B角: ${item.res_b_name}`

      return item
    },
    beforeClose() {
      this.$emit('update:visible', false)
    },
  },
}
</script>

<style lang="scss" scoped></style>
